﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AnesSystem.ViewModels;
using AnesSystem.Models;
using System.Collections.ObjectModel;
using AnesSystem.Util;

namespace AnesSystem.Controls
{
    /// <summary>
    /// 生命体征图标显示主界面
    /// </summary>
    public partial class RealTimeItemCtrl : UserControl
    {

        #region Variables

        private RealTimeItemCtrlViewModel _model;
        Point pBefore = new Point();//鼠标点击前坐标
        Point eBefore = new Point();//圆移动前坐标
        bool isSelected = false;//是否需要移动
        private VitalTipPopup _popup;
        private PatientVitalSignsModel _tip;
        private PatientVitalSignsModel _selected;
        private PatientVitalSignsModel _curPat;

        public PatientVitalSignsModel CurPat
        {
            get
            {
                return _curPat;
            }

            set
            {
                _curPat = value;
            }
        }
        #endregion

        #region Constructor

        public RealTimeItemCtrl()
        {
            InitializeComponent();
            _popup = new VitalTipPopup();
            _tip = new PatientVitalSignsModel();
            _popup.DataContext = _tip;
            this.Loaded += RealTimeItemCtrl_Loaded;
        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        public event EventHandler ParameterBtnClicked;

        #endregion

        #region Private Methods


        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ParameterBtnClicked?.Invoke(sender, e);
        }

        private void _canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
          
            if (e.ClickCount==2)
            {
                return;
            }
            if (e.Source is TableCellCtrl)
            {
                return;
            }
            if (e.Source.GetType().BaseType != null && e.Source.GetType().BaseType == typeof(RealVitalIcon))
            {
                RealVitalIcon el = (RealVitalIcon)e.Source;
                PatientVitalSignsModel pm = el.DataContext as PatientVitalSignsModel;
                Point p = e.GetPosition(null);//获取鼠标移动中的坐标
                el.ChangeData(eBefore.Y + (p.Y - pBefore.Y));
                el.ReleaseMouseCapture();//鼠标释放
                isSelected = false;
            }
        }

        private void _canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Source is TableCellCtrl)
            {
                return;
            }
            if (e.Source.GetType().BaseType != null && e.Source.GetType().BaseType == typeof(RealVitalIcon))
            {
                RealVitalIcon el = (RealVitalIcon)e.Source;
                PatientVitalSignsModel pm = el.DataContext as PatientVitalSignsModel;
                el.ToolTip = string.Format("时间:{0}\r\n{1}:{2}", pm.CaptureTime.ToString(DateFormat.DATE_FORMAT_STR_HM), pm.Name, pm.ModValue);
                //_tip.Name = pm.Name;
                //_tip.ModValue = pm.ModValue;
                //_tip.CaptureTime = pm.CaptureTime;
                //_popup.IsOpen = true;
                //_popup.PlacementTarget = el;
                if (e.LeftButton == MouseButtonState.Pressed && isSelected)
                {
                    Point p = e.GetPosition(null);//获取鼠标移动中的坐标
                    double left = Canvas.GetLeft(el);
                    double top = eBefore.Y + (p.Y - pBefore.Y);
                    el.SetPoint(left, top);
                    _tip.ModValue = el.CalY2Valueplot(eBefore.Y + (p.Y - pBefore.Y)).ToString();
                }
            }
        }

        private void _canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                _model.ClickShowDialog(CurPat);
            }
            if (e.Source is TableCellCtrl)
            {
                return;
            }
            if (e.Source.GetType().BaseType == typeof(RealVitalIcon))
            {
                this.pBefore = e.GetPosition(null);//获取点击前鼠标坐标
                RealVitalIcon el = (RealVitalIcon)e.Source;
                CurPat = el.DataContext as PatientVitalSignsModel;
                this.eBefore = new Point(Canvas.GetLeft(el), Canvas.GetTop(el));//获取点击前圆的坐标
                el.CaptureMouse();//鼠标捕获此圆
                isSelected = true;
            }
            e.Handled = false;
        }

        private void RealTimeItemCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            _model = this.DataContext as RealTimeItemCtrlViewModel;
            if (_model != null)
                _model.InitTWMMHGLocation();
        }

        private void _realTimeCanvas_MouseLeave(object sender, MouseEventArgs e)
        {
            //_popup.IsOpen = false;
        }

        private void _realTimeCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //if (e.ClickCount == 2)
            //{
            //    ParameterBtnClicked?.Invoke(sender, e);
            //}
        }



        #endregion
    }
}
